package cn.bytecollege.controller;
import cn.bytecollege.model.Orders;
import cn.bytecollege.model.User;
import cn.bytecollege.service.DiliveryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 物资配送人员Controller
 */
@Controller
@RequestMapping("dilivery")
public class DiliveryController {
    @Autowired
    private DiliveryService diliveryService;

    /**
     * 骑手获取订单信息
     * @return
     */
    @GetMapping("get")
    public String get(Model model){
        List<Orders> list = diliveryService.get();
        model.addAttribute("list",list);
        return "riderOrder";
    }

    /**
     * 骑手接单
     * @param model
     * @param session
     * @param orderId
     * @return
     */
    @GetMapping("modById")
    public String modById(Model model, HttpSession session, Integer orderId){
        //获取当前骑手的userId
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        //将当前骑手的userId放入orders表中
        int i = diliveryService.mod(userId, orderId);
        //如果失败
        if (i==0){
            String msg = "这个订单已经被其他骑手抢先一步啦~~~";
            model.addAttribute("msg",msg);
        }
        //成功返回1
        return "redirect:get";
    }

    /**
     * 骑手查看自己订单
     * @param session
     * @return
     */
    @GetMapping("getById")
    public String getById(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
//        Integer userId = 4;
        List<Orders> list = diliveryService.getById(userId);
        model.addAttribute("list",list);
        return "riderMyOrder";
    }

    /**
     * 骑手删除订单
     * @param orderId
     * @return
     */
    @GetMapping("delOrderById")
    public String delOrderById(Integer orderId){
        diliveryService.delOrderById(orderId);
        return "redirect:getById";
    }
}
